import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/common/detail_item.dart';
import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ExpandCollapse extends StatefulWidget {
  ///显示的高度比例，默认0.5(值为大于等于1的数值时则为展开状态)
  final double heightFactor;

  ///左侧文本
  final String leftText;

  ///右侧文本
  final String rightText;

  ///子组件列表
  final List<Widget>? children;

  const ExpandCollapse(
      {super.key,
      this.heightFactor = 0.5,
      this.leftText = '',
      this.rightText = '展开',
      this.children});

  @override
  State<StatefulWidget> createState() {
    return _ExpandCollapseState();
  }
}

class _ExpandCollapseState extends State<ExpandCollapse> {
  //是否展开
  double heightRatio = 0.5;

  @override
  void initState() {
    heightRatio = widget.heightFactor;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ///顶部标题
        Container(
          padding: EdgeInsets.only(left: 10.w, right: 10.w, top: 10.h),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(widget.leftText,
                  style: setTextStyle(
                      14, FontWeight.w700, ThemeConfig.mainTextColor)),
              Visibility(
                  visible: heightRatio >= 1 ? false : true,
                  child: BaseInteractive(
                    onClick: () {
                      setState(() {
                        heightRatio = 1;
                      });
                    },
                    childWidget: Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: [
                        Text(widget.rightText,
                            style: setTextStyle(13, FontWeight.w400,
                                ThemeConfig.sectionTextColor)),
                        Icon(
                          Icons.expand_more,
                          size: 14,
                        )
                      ],
                    ),
                  ))
            ],
          ),
        ),
        Container(
          padding: EdgeInsets.only(bottom: 10.w),
          child: ClipRect(
            child: Align(
              alignment: Alignment.topCenter,
              heightFactor: heightRatio,
              child: Column(
                children: _getChildren(),
              ),
            ),
          ),
        )
      ],
    );
  }

  ///获取子组件列表
  List<Widget> _getChildren() {
    List<Widget> arr = [];
    for (int i = 0; i < widget.children!.length; i++) {
      arr.add(widget.children![i]);
    }
    arr.add(_collapse());
    return arr;
  }

  ///折叠
  Widget _collapse() {
    return BaseInteractive(
      childWidget: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text('收起',
              style: setTextStyle(
                  13, FontWeight.w400, ThemeConfig.sectionTextColor)),
          Icon(Icons.expand_less)
        ],
      ),
      onClick: () {
        setState(() {
          heightRatio = widget.heightFactor >= 1 ? 0.5 : widget.heightFactor;
        });
      },
    );
  }
}
